﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using IService.Business;
using System.Data;

namespace IService.Handler
{
	/// <summary>
	/// Summary description for TypeHandler
	/// </summary>
	public class TypeHandler : IHttpHandler
	{
		private readonly string JsonFormatter = "{{'id':{0},'name':'{1}'}}";

		public void ProcessRequest(HttpContext context)
		{
			context.Response.ContentType = "text/plain";
			context.Response.Charset = "utf-8";
			string json = string.Empty;
			switch (context.Request["oper"])
			{
				case null:
					json = GetNavData();
					break;
			}
			context.Response.Write(json);
		}

		/// <summary>
		/// 获取类别
		/// </summary>
		/// <returns></returns>
		private string GetNavData()
		{
			DataTable dt = DTVBusiness.TypeManager.GetTypeListAll();
			StringBuilder sb = new StringBuilder();
			
			try
			{
				sb.Append("[");
				bool bAppendCommo = false;
				foreach (DataRow dr in dt.Rows)
				{
					if (bAppendCommo)
						sb.Append(",");
					sb.AppendFormat(JsonFormatter,
						dr["ID"].ToString(),
						dr["TypeName"].ToString());
					bAppendCommo = true;
				}
				sb.Append("]");
			}
			catch
			{ }
			return sb.ToString();
		}


		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}
}